#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\x\y')

import glob, re, string, sys, os
import pepe.p_txt.bigtxt as bt

def join(flsmask_s, csvColDel_s='|', skipHeader_b="0"):
    o_fl = open('txt-join.out','w')
    l_fl = open('txt-join_log.out','w')
    llenght_h = {}
    ccd_h = {}
    for infile_s in glob.glob(flsmask_s):
        flcnt_n = 1
        for line_s in bt.lines(infile_s):

            # write lines to output
            if flcnt_n == 1 and skipHeader_b == "1":
                pass
            else:
                if len(re.sub('\n','',line_s)) > 0:
                    o_fl.write(string.strip(line_s)+'\n')

            #check and record - csv delimiter integrity
            if not csvColDel_s == '':
                ccd_n = string.count(line_s,csvColDel_s)
                if ccd_h.get(ccd_n):
                    ccd_h[ccd_n][0] += 1 
                else:
                    ccd_h[ccd_n] =      [   1,
                                            infile_s,
                                            flcnt_n,
                                            line_s[:-1]]

            #check and record - lines lengths
            if llenght_h.get(len(line_s)):
                llenght_h[len(line_s)][0] += 1
            else:
                llenght_h[len(line_s)] = [  1,
                                            infile_s,
                                            flcnt_n,
                                            line_s[:-1]]
            flcnt_n += 1
    o_fl.close()
    l_fl.write ( '-'*80+'\nlines lengths:\n'+'-'*80 + '\n' )
    for k in sorted(llenght_h.keys(), reverse=True):
        l_fl.write( '|len >~'      + string.rjust(str(k),9) + 
                    '|cnt >~'      + string.rjust(str(llenght_h[k][0]),9) +
                    '|infile >~'   + string.rjust(str(llenght_h[k][1]),20) +
                    '~'            + string.rjust(str(llenght_h[k][2]),9) +
                    '|ifline >~'   + string.rjust(str(llenght_h[k][3]),9) +
                    '\n'
                    )
    l_fl.write ( '-'*80+'\ncsv delimiter anomalies:\n'+'-'*80 + '\n' )
    for k in sorted(ccd_h.keys(), reverse=True):
        l_fl.write( '|cnt >~'      + string.rjust(str(k),9) + 
                    '|cnt >~'      + string.rjust(str(ccd_h[k][0]),9) +
                    '|infile >~'   + string.rjust(str(ccd_h[k][1]),20) +
                    '~'            + string.rjust(str(ccd_h[k][2]),9) +
                    '|ifline >~'   + string.rjust(str(ccd_h[k][3]),9) +
                    '\n'
                    )
    l_fl.close()
    #os.system('start notepad _join-txt-files_log.xyz')
    return 1

HELP = """
Purpose:    Join text files.
Usage:      txt-join INPFILES CDELIMITER SKIPHEADER
Example:    txt-check *.csv "|" \"1\"
"""

def main():
    if len(sys.argv) == 3:
        join(sys.argv[1], sys.argv[2], 0)
    elif len(sys.argv) == 4:
        join(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        print HELP

if __name__ == "__main__":
    main()
